package com.jingyanzi.member.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jingyanzi.member.api.EmployeeClientAPI;
import com.jingyanzi.member.dao.MSchedulingMapper;
import com.jingyanzi.member.domain.EmployeeDO;
import com.jingyanzi.member.domain.SchedulingDO;
import com.jingyanzi.member.domain.result.ResultDO;
import com.jingyanzi.member.domain.result.ResultSupport;
import com.jingyanzi.member.entity.MScheduling;
import com.jingyanzi.member.entity.MSchedulingExample;
import com.jingyanzi.member.entity.MSchedulingExample.Criteria;
import com.jingyanzi.member.query.SchedulingQueryDO;
import com.jingyanzi.member.service.ISchedulingService;
import com.jingyanzi.member.util.BeanUtilsExtends;


@Service("schedulingService")
public class SchedulingServiceImpl extends BaseServiceImpl implements ISchedulingService {

	@Autowired
	private EmployeeClientAPI employeeClientAPI;

	@Autowired
	private MSchedulingMapper schedulingMapper;

	@Override
	public ResultDO add(SchedulingDO domain) {

		MScheduling entity = new MScheduling();
		ResultDO result = BeanUtilsExtends.copy(entity, domain);

		if (!result.isSuccess()) {
			result.setErrorMsg("插入客户家庭联系人错误对象复制错误");
			return result;
		}

		// 查重
		entity.setSchedDate(new Date(Long.parseLong(domain.getSchedDateStamp())));

		if (null != domain.getIds() && domain.getIds().length > 0) {
			Integer[] ids = domain.getIds();

			MSchedulingExample example = new MSchedulingExample();
			MSchedulingExample.Criteria criteria = example.createCriteria();
			criteria.andEmployeeIdIn(Arrays.asList(ids));
			criteria.andSchedDateEqualTo(new Date(Long.parseLong(domain.getSchedDateStamp())));

			int count = schedulingMapper.countByExample(example);
			if (count > 0) {
				result.setErrorMsg("客户已排班");
				return result;
			}
			for (int i = 0; i < ids.length; i++) {
				Integer employeeId = ids[i];
				EmployeeDO domain2 = employeeClientAPI.get(employeeId);
				entity.setId(null);
				entity.setEmployeeId(employeeId);
				entity.setEmployeeName(domain2.getRealName());
				entity.setSortNum(i + 1);
				int ret = -1;
				try {
					ret = schedulingMapper.insertSelective(entity);
				} catch (Exception e) {
					e.printStackTrace();
					ret = -1;
				}
				if (ret < 0) {
					result.setErrorMsg("插入失败");
					result.setSuccess(false);
				}
			}
		}

		return result;
	}

	@Override
	public ResultDO modifi(SchedulingDO domain) {

		MScheduling entity = new MScheduling();
		ResultDO result = BeanUtilsExtends.copy(entity, domain);

		if (!result.isSuccess()) {
			result.setErrorMsg("插入客户家庭联系人错误对象复制错误");
			return result;
		}

		int ret = -1;

		try {
			ret = schedulingMapper.updateByPrimaryKeySelective(entity);
		} catch (Exception e) {
			e.printStackTrace();
			ret = -1;
		}

		if (ret < 0) {
			result.setErrorMsg("插入失败");
			result.setSuccess(false);
		}

		return result;
	}

	@Override
	public ResultDO get(Integer id) {

		ResultDO result = new ResultSupport();

		MScheduling entity = null;

		try {
			entity = schedulingMapper.selectByPrimaryKey(id);
		} catch (Exception e) {
			e.printStackTrace();
		}

		if (entity != null) {
			result.setErrorMsg("插入失败");
			result.setSuccess(false);
			return result;
		}

		result.setModel(ResultDO.FIRST_MODEL_KEY, getSchedulingDO(entity));
		return result;
	}

	@Override
	public ResultDO remove(Integer[] ids) {

		ResultDO result = new ResultSupport();

		if (null == ids || ids.length < 1) {
			return result;
		}

		MSchedulingExample example = new MSchedulingExample();
		MSchedulingExample.Criteria criteria = example.createCriteria();
		criteria.andIdIn(Arrays.asList(ids));

		int r = 1;

		try {
			r = schedulingMapper.deleteByExample(example);
		} catch (Exception e) {
			e.printStackTrace();
		}

		if (r < 0) {
			result.setErrorMsg("删除失败");
			result.setSuccess(false);
			return result;
		}

		return result;
	}

	public ResultDO checkEmployee(SchedulingQueryDO query) {

		ResultDO result = new ResultSupport();

		MSchedulingExample example = new MSchedulingExample();

		Criteria criteria = example.createCriteria();

		criteria.andEmployeeNameEqualTo(query.getName());
		int count = 0;
		try {
			count = schedulingMapper.countByExample(example);
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			return result;
		}

		result.setModel(ResultDO.FIRST_MODEL_KEY, count);
		return result;
	}

	@Override
	public ResultDO query(SchedulingQueryDO query) {

		ResultDO result = new ResultSupport();

		List<MScheduling> list = null;

		MSchedulingExample example = new MSchedulingExample();

		Criteria criteria = example.createCriteria();

		if (StringUtils.isNotEmpty(query.getName())) {
			criteria.andEmployeeNameLike("%" + query.getName() + "%");
		}
		if (StringUtils.isNotEmpty(query.getSchedDateStamp())) {
			Date startTime = new Date(Long.parseLong(query.getSchedDateStamp()));
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			try {
				startTime = sdf.parse(sdf.format(startTime));
				criteria.andSchedDateEqualTo(startTime);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		
		example.setOrderByClause(" sortNum asc ");
		
		if (query.isPage()) {
			example.setLimitByPage(query.getStart() + "," + query.getPageRows());
			long count = schedulingMapper.countByExample(example);
			result.setModel(ResultSupport.SECOND_MODEL_KEY, count);
		}

		try {
			list = schedulingMapper.selectByExample(example);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

		result.setModel(ResultDO.FIRST_MODEL_KEY, getSchedulingDOList(list));
		return result;
	}

	/**
	 * 对象拷贝单个实体到对象中
	 * 
	 * @param obj
	 * @return 返回DO对象
	 */
	public SchedulingDO getSchedulingDO(MScheduling entity) {
		SchedulingDO dst = new SchedulingDO();
		if (entity == null)
			return dst;
		return BeanUtilsExtends.copyProperties(dst, entity) ? dst : null;
	}

	/**
	 * 对象拷贝多个实体到列表中
	 * 
	 * @param List
	 * @return List
	 */
	public List<SchedulingDO> getSchedulingDOList(List<MScheduling> list) {
		List<SchedulingDO> result = new ArrayList<SchedulingDO>();
		if (list != null && !list.isEmpty()) {
			for (MScheduling entity : list) {
				SchedulingDO domain = getSchedulingDO(entity);
				if (domain != null) {
					result.add(domain);
				}
			}
		}
		return result;
	}

}
